<!DOCTYPE html>
<html>
  <head>
    <title>
      audiobuffersource-detune-modulation.html
    </title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script src="../resources/audit-util.js"></script>
    <script src="../resources/audit.js"></script>
    <script src="../resources/audiobuffersource-testing.js"></script>
    <script src="../resources/buffer-loader.js"></script>
    <script src="../resources/audio-file-utils.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let sampleRate = 44100;
      let duration = 0.25;

      let context =
          new OfflineAudioContext(1, sampleRate * duration, sampleRate);
      let referenceBuffer;
      let audioBufferResult;

      let audit = Audit.createTaskRunner();

      // Task: Load the reference file asynchronously. In order to create a new
      // reference file, use the task 'generate-reference' below.
      audit.define('load-reference', (task, should) => {
        let loader = new BufferLoader(
            context, ['resources/audiobuffersource-detune-modulation-expected.wav'],
            function(bufferList) {
              referenceBuffer = bufferList[0];
              task.done();
            });

        loader.load();
      });


      // Task: Render the actual buffer and compare with the reference.
      audit.define('generate-verify', (task, should) => {

        // With this setting, the detune will be changing continuously and
        // repeatedly within the range of [-1200, 1200] around 440Hz, based on
        // the input from the oscillator.
        createSawtoothWithModulation(context, 'detune', 440, 1200);

        context.startRendering()
            .then(function(renderedBuffer) {
              audioBufferResult = renderedBuffer;
              let actual = renderedBuffer.getChannelData(0);
              let expected = referenceBuffer.getChannelData(0);

              // Compare two buffers with arbitrary (yet reasonable)
              // constraints. There parameters are determined by try bot
              // experiments.
              compareBuffersWithConstraints(should, actual, expected, {
                prefix: '',
                thresholdSNR: 93.320,
                thresholdDiffULP: 1.0352,
                thresholdDiffCount: 0,
                bitDepth: 16
              });

              let filename = 'resources/audiobuffersource-detune-modulation-actual.wav';
              if (downloadAudioBuffer(renderedBuffer, filename))
                should(true, 'Saved reference file').message(filename, '');
            })
            .then(() => task.done());
      });

      audit.run();
    </script>
  </body>
</html>
